package android.app.gfestival;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.gfestival.adapter.DbHelper;
import android.app.gfestival.component.ArtistViewSwicher;
import android.app.gfestival.soap.Artist;
import android.app.gfestival.soap.ArtistDescription;
import android.app.gfestival.soap.SoapParserHelper;
import android.app.gfestival.utils.DateTimeUtil;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.db.service.DatabaseService;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

public class ArtistActivity extends Activity{
	private static final String TAG = "ArtistActivity";
	private static final String URL_RSS = "http://www.gfestival.fo/index.php?id=1&type=100";
	private ArtistViewSwicher view;
	final DbHelper helper = new DbHelper(this);
	private ProgressDialog dialog;
	private DatabaseService service;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dialog = showProgressDialog();
		service = new DatabaseService(this);
		new ArtistsAsynTask().execute(null);
		
	}
	
	public ProgressDialog showProgressDialog() {
		ProgressDialog progress = new ProgressDialog(this.getParent(), R.style.ProgressDialogSpinner);
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setMessage("Loading...");
		progress.show();
		return progress;
	}
	
	@Override
	protected void onDestroy() {
		if(service != null) service.onDestroy();
		super.onDestroy();
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode != KeyEvent.KEYCODE_BACK) return super.onKeyDown(keyCode, event);
		if(view.showPreviou() < 0 ){
			getParent().finish();
		}
		return false;
	}
	
	private class ArtistsAsynTask extends AsyncTask<String, String, Cursor>{

		@Override
		protected Cursor doInBackground(String... params) {
			
			return loadData();
		}
		
		@Override
		protected void onPostExecute(Cursor result) {
			// TODO Auto-generated method stub
			showData(result);
		}
	}

	public void showData(Cursor result) {
		if(result != null){
			view = new ArtistViewSwicher(this, result, service);
			setContentView(view);
		}
		dialog.dismiss();
	}

	public Cursor loadData() {
		
		List<ArtistDescription> artists = new ArrayList<ArtistDescription>();
		Cursor cursor = null;
		try {
			if(service.isInternetConnected()){
				artists = SoapParserHelper.getAllArtistHaveEvents();
				service.getArtistDescriptionDAO().deleteAll();
				service.getArtistDAO().deleteAll();
				for(int i = 0; i < artists.size(); i++){
					Artist artist = SoapParserHelper.getArtistDetailById(artists.get(i).artist_id).get(0);
					if(artist.event_day == null) artist.event_day = "1/1/2000";
					if(artist.artist_start_time.equals("null")){
						artist.artist_start_time = "";
					}
					String time = artist.event_day + " " + artist.artist_start_time;
					time = DateTimeUtil.convertDate(time ,"EEEE HH:mm");
					time = DateTimeUtil.convertDay(time, this);
					artists.get(i).artist_start_time = time;
					artists.get(i).event_day = artist.event_day;
					artists.get(i).event_place_name = artist.event_place_name;
					service.getArtistDAO().insert(artist);
				}
				service.getArtistDescriptionDAO().insertList(artists);
			}
			cursor = service.getArtistDescriptionDAO().findAllReturnCursor("artist_name");
		} catch (Exception e) {
			e.printStackTrace();
		}
		startManagingCursor(cursor);
		return cursor;
	}
}
